//
//  ViewController.m
//  自定义转场动画
//
//  Created by 唐铁山 on 2018/9/11.
//  Copyright © 2018年 唐铁山. All rights reserved.
//

#import "ViewController.h"
#import "TSControllerTransition.h"
#import "TSPresentTransition.h"
#import "TSPresentViewController.h"
#import "TSDismissTransition.h"

@interface ViewController ()<UINavigationControllerDelegate, UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) UIView *transitionView;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // CGAffineTransition
    _transitionView = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 80, 80)];
    _transitionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_transitionView];
    
    
    /**
     设置UIView的transform属性，会改变UIView的Frame，但是不会改变bounds
    
     */
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 60, 80, 80)];
    _imageView.image = [UIImage imageNamed:@"6.jpg"];
    [self.view addSubview:_imageView];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(20, 400);
    transform = CGAffineTransformRotate(transform, M_PI * 3);
    transform = CGAffineTransformScale(transform, 1.5, 1.5f);
    [UIView animateWithDuration:3.0f animations:^{
        self.imageView.transform = transform;
    } completion:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.imageView.transform = CGAffineTransformIdentity;
}

- (void)addScaleAndRotation {
    UIImageView *demoView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 60, 80, 80)];
    demoView.image = [UIImage imageNamed:@"6.jpg"];
    [self.view addSubview:demoView];
    
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_4);
    transform = CGAffineTransformScale(transform, 1.5, 1.5f);
    demoView.transform = transform;
}

- (void)addTranslation {
    UIImageView *demoView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 60, 80, 80)];
    demoView.image = [UIImage imageNamed:@"6.jpg"];
    [self.view addSubview:demoView];
    
    demoView.transform = CGAffineTransformMakeTranslation(20, 80);
}

- (void)addRotation {
    /**
     设置UIView的transform属性，会改变UIView的Frame，但是不会改变bounds
     
     */
    UIImageView *demoView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 60, 80, 80)];
    demoView.image = [UIImage imageNamed:@"6.jpg"];
    [self.view addSubview:demoView];
    
    // 做旋转时，如果角度为正数，则进行顺势针旋转，反之，则为逆时针旋转
    demoView.transform = CGAffineTransformMakeRotation(M_PI_4);
}

- (void)addScale {
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(140, 60, 80, 80)];
    demoView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:demoView];
    
    demoView.transform = CGAffineTransformMakeScale(0.8f, 1.2f);
    NSLog(@"demoView.frame = %@", NSStringFromCGRect(demoView.frame));
    NSLog(@"demoView.bounds = %@", NSStringFromCGRect(demoView.bounds));
}

- (void)addCorner {
    // 添加圆角
    UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:_transitionView.bounds byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = shapePath.CGPath;
    shapeLayer.frame = _transitionView.bounds;
    _transitionView.layer.mask = shapeLayer;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)presentButtonClicked:(id)sender {
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    TSPresentViewController *presentedViewController = [storyBoard instantiateViewControllerWithIdentifier:@"TSPresentViewController"];
    presentedViewController.transitioningDelegate = self;
//    presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:presentedViewController animated:YES completion:nil];
    
}

#pragma mark - UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {

    if (operation == UINavigationControllerOperationPush) {
        TSControllerTransition *transition = [TSControllerTransition new];
        transition.push = YES;
        return transition;
    }

    return nil;
}


#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    // present代理方法
    TSPresentTransition *presentTransition = [TSPresentTransition new];
    
    return presentTransition;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    // dismiss代理方法
    TSDismissTransition *dismissTransition = [TSDismissTransition new];
    return dismissTransition;
}
//
//- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
//    return nil;
//}



@end
